﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using Rework.CommerceHistory.Models;
using Orchard;
using Orchard.ContentManagement;
using Orchard.Environment.Extensions;

namespace Rework.CommerceHistory.Services {
    [OrchardFeature("Rework.CommerceHistory")]
    public class CommerceHistory : ICommerceHistory {
        public IOrchardServices Services { get; set; }

        public CommerceHistory(
            IOrchardServices services
            ) {
            Services = services;
        }

        public IEnumerable<CommerceHistoryPart> GetProducts(int userId) {
            return Services.ContentManager.Query<CommerceHistoryPart, CommerceHistoryPartRecord>()
                .Where(ch => ch.UserId == userId)
                .List();
        }

        public void AddProducts(IEnumerable<CommerceHistoryPart> commerceHistoryParts) {
            foreach (var product in commerceHistoryParts) {
                AddProduct(product);
            }
        }

        public void AddProduct(CommerceHistoryPart commerceHistoryPart) {
            Services.ContentManager.Create(commerceHistoryPart);
        }
    }
}